<template>
	<view class="page">
		<view class="money-box">
			<view class="money-item">
				<text>余额</text>
				<text>{{money}}</text>
			</view>
			<view class="money-item">
				<text>积分</text>
				<text>{{integral}}</text>
			</view>
		</view>
		<view class="form-box">
			<view class="form-item">
				<text class="form-title">兑换类型</text>
				<view class="check-box">
					<text @click="check(1)" :class="pay_class==1?'active':''">余额+积分</text>
					<text @click="check(2)" :class="pay_class==2?'active':''">余额</text>
				</view>
			</view>
			<view class="form-item">
				<view class="form-title-group">
					<text>兑换数量</text>
					<input type="number" v-model="exchange_num" placeholder="必填" @input="calculate()" />
				</view>
				<text></text>
			</view>
			<view class="form-item">
				<text>所需数量</text>
				<text style="color: #F63F3D;">余额:<text>{{need_money}}</text></text>
				<text>积分:<text>{{need_jifen}}</text></text>
			</view>
		</view>
		<button type="default" class="sue-btn" @click="submit()">提交</button>
		<jpKeyboard ref="jpKeyboard1" keyType="two" @inputPwd="Getpassword" @backspace="del_password"></jpKeyboard>
	</view>
</template>

<script>
	import jpKeyboard from '@/components/jp-keyboard/jp-keyboard.vue';

	export default {
		components: {
			jpKeyboard
		},
		data() {
			return {
				money: 0,
				integral: 0,
				exchange_num: '',
				pay_class: 1,
				exchange_rate: 0.8, //比例
				need_money: 0,
				need_jifen: 0,
				password: '',
				exchange_type:{
					1 : 'point_and_balance',
					2 : 'balance',
				}
			}
		},
		onLoad() {
			this.Getmsg()
			this.Getexchange_rate()
		},
		methods: {

			toOpen1() {

			},

			check(e) {
				this.pay_class = e
				this.calculate()
			},
			// 计算
			calculate() {
				if (this.exchange_num == '') {
					this.need_money = 0
					this.need_jifen = 0
				} else if (this.pay_class == 1) {
					let need1 = parseFloat(this.exchange_num) * parseFloat(this.exchange_rate)
					this.need_money = need1.toFixed(2)
					let need2 = parseFloat(this.exchange_num) * (1 - parseFloat(this.exchange_rate))
					this.need_jifen = need2.toFixed(2)
				} else if (this.pay_class == 2) {
					this.need_money = this.exchange_num
					this.need_jifen = 0
				}
			},
			Getmsg() {
				this.$api.sendRequest({
					url: '/api/member/info',
					success: res => {
						if (res.code == 0) {
							console.log(res)
							this.money = res.data.balance
							this.integral = res.data.point
						}
					}
				});
			},
			Getexchange_rate() {
				this.$api.sendRequest({
					url: '/api/Ticketchange/config',
					success: res => {
						if (res.code == 0) {
							console.log(res)
							this.exchange_rate = parseFloat(res.data.value.change_rate) / 100
							console.log(this.exchange_rate)
						}
					}
				});
			},
			submit() {
				if (this.exchange_num == 0) {
					uni.showToast({
						title: '请输入兑换数量~',
						icon: 'none'
					})
				} else if (this.exchange_num != 0 && this.pay_class == 1) {
					if (Number(this.need_money) > Number(this.money)) {
						uni.showToast({
							title: '余额不足~',
							icon: 'none'
						})
					} else {
						this.$refs.jpKeyboard1.toOpen()
					}
				} else if (this.exchange_num != 0 && this.pay_class == 2) {
					if (Number(this.need_money) > Number(this.money)) {
						uni.showToast({
							title: '余额不足~',
							icon: 'none'
						})
					} else if (Number(this.need_money) <= Number(this.money) && Number(this.need_jifen) > Number(this.integral)) {
						uni.showToast({
							title: '积分不足~',
							icon: 'none'
						})
					} else if (Number(this.need_money) <= Number(this.money) && Number(this.need_jifen) <= Number(this.integral)) {
						this.$refs.jpKeyboard1.toOpen()
					} else {
						uni.showToast({
							title: '余额不足~',
							icon: 'none'
						})
					}
				}
			},
			// 输入密码完成
			Getpassword(e) {
				console.log(e)
				this.password = e
				this.sue_submit()
			},
			del_password() {},
			// 提交
			sue_submit() {
				let exchange_type = this.exchange_type[this.pay_class];
				this.$api.sendRequest({
					url: '/api/Ticketchange/applyChange',
					data: {
						transfer_type: exchange_type,
						transfer_money: this.exchange_num,
						point: this.need_jifen,
						balance: this.need_money,
						pay_password: this.password
					},
					success: res => {
						if (res.code == 0) {
							console.log(res)
							uni.showToast({
								title: '兑换成功~',
							})
							setTimeout(() => {
								this.$util.goBack();
							}, 500)
						} else {
							if (res.message == 'PAY_PASSWORD_ERROR') {
								uni.showToast({
									title: '密码错误',
									icon: 'none'
								})
							} else {
								uni.showToast({
									title: res.message,
									icon: 'none'
								})
							}

						}
					},
					fail: (err) => {
						uni.showToast({
							title: res.message,
							icon: 'none'
						})
					}
				});
			}
		}
	}
</script>

<style lang="scss">
	.page {
		background-color: white;
		height: 100vh;

		.money-box {
			height: 250rpx;
			// background-image: linear-gradient(to bottom right, #ED018C, #EB2C9D);
			background-color: #FF335C;
			display: flex;
			align-items: flex-end;
			justify-content: space-between;

			.money-item {
				color: white;
				font-size: 35rpx;
				display: flex;
				align-items: flex-start;
				flex-direction: column;
				padding: 0 23rpx;
				padding-bottom: 20rpx;

				text:last-child {
					font-size: 42rpx;
				}
			}
		}

		.form-box {
			padding: 0 23rpx;
			padding-top: 30rpx;
			padding-bottom: 60rpx;

			.form-item {
				border-bottom: 2rpx solid #F8F8F8;
				height: 70rpx;
				line-height: 70rpx;
				display: flex;
				align-items: center;
				padding: 18rpx 23rpx;
				padding-left: 0;
				margin-left: 23rpx;
				justify-content: space-between;

				.form-title {
					font-size: 30rpx;
				}

				.check-box {
					text {
						margin-left: 14rpx;
						border: 2rpx solid #4385FF;
						padding: 4rpx 10rpx;
						color: #4385FF;
					}

					.active {
						color: white;
						background-color: #4385FF;
					}
				}

				.form-title-group {
					display: flex;
					align-items: center;

					text {
						font-size: 30rpx;
					}

					input {
						font-size: 28rpx;
						margin-left: 25rpx;
					}
				}

				text {
					font-size: 30rpx;
				}
			}
		}

		.sue-btn {
			background-image: linear-gradient(to right, #F63F3D, #ED018A);
			border-radius: 10rpx;
			margin: 0 23rpx;
			color: white;
			font-size: 32rpx;
			letter-spacing: 4rpx;
		}
	}
</style>
<style scoped>

</style>
